<?php

use App\Models\GoodsPicPrimaryModel;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\RequestOptions;

/**
 * CURL模拟GET
 * @param string $url
 * @param array $header
 * @return bool|string
 */
function curlGet(string $url, array $header = []): bool|string
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    !empty($header) && curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}

/**
 * CURL模拟POST
 * @param string $url
 * @param array $header
 * @param array $data
 * @return bool|string
 */
function curlPost(string $url, array $header, array $data): bool|string
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    !empty($header) && curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}

/**
 * 商品规格数组分组
 * @param array $specData
 * @param bool $decode
 * @param string $decodeKey
 * @param array $exceptKeys
 * @return array
 */
function groupGoodsData(array $specData, bool $decode, string $decodeKey, array $exceptKeys = []): array
{
    $data = [];
    $goodsIds = array_column($specData, 'goods_id');
    $goodsCollection = GoodsPicPrimaryModel::whereIn('ID', $goodsIds)->select(['ID', 'PName', 'Path', 'FName', 'IsEncry'])->get();
    foreach ($specData as $item) {
        if ($item) {
            if ($decode) {
                $decodeInfo = json_decode($item[$decodeKey]);
                foreach ($decodeInfo as $key => $value) {
                    if (!in_array($key, $exceptKeys))
                        $item[$key] = $value;
                }
                unset($item[$decodeKey]);
            }
            if (!isset($data[$item['goods_id']])) {
                $goods = $goodsCollection->where('ID', $item['goods_id'])->first();
                $data[$item['goods_id']] = [
                    'goods_id'      => $item['goods_id'],
                    'goods_specs'   => [$item],
                    'goods_name'    => $goods->PName,
                    'thumbnail'     => $item['thumbnail'],
                    'cover'         => $item['thumbnail']
                ];
            }
            else
                $data[$item['goods_id']]['goods_specs'][] = $item;
        }
    }
    return array_values($data);
}

/**
 * Guzzle Post 表单
 * @param string $host
 * @param string $path
 * @param array $data
 * @param array $headers
 * @return string
 * @throws GuzzleException
 */
function GuzzlePost(string $host, string $path, array $data, array $headers = []): string
{
    $client = getGuzzleClient($host, $headers);
    $response = $client->request('POST', $path, ['form_params' => $data]);
    return (string)$response->getBody();
}

/**
 * Guzzle Post Json
 * @param string $host
 * @param string $path
 * @param array $data
 * @param array $headers
 * @return string
 * @throws GuzzleException
 */
function GuzzlePostJson(string $host, string $path, array $data, array $headers = []): string
{
    $client = getGuzzleClient($host, $headers);
    $response = $client->request('POST', $path, [RequestOptions::JSON => $data]);
    return (string)$response->getBody();
}

/**
 * 创建Guzzle客户端
 * @param string $host
 * @param array $headers
 * @return Client
 */
function getGuzzleClient(string $host, array $headers = []): Client
{
    $config['base_uri'] = $host;
    $config['timeout'] = 3.0;
    if (!empty($headers))
        $config['headers'] = $headers;
    return new Client($config);
}
